<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\ForumNoteCateModel;
use think\Controller;
use think\Db;
use think\Session;


class Forum extends Base
{
    /*************************帖子分类管理***开始**************************/
    /**
     * 帖子分类
     */
    public function noteCate()
    {
        if (request()->isAjax()) {
            $param = input("param.");
            $where = [];
            $limit = $param['limit'];
            $offset = ($param['page'] - 1) * $limit;
            $cate = new ForumNoteCateModel();
            $cate_list = $cate->getAllCates($where, $offset, $limit);
            foreach ($cate_list as $key => $vo) {
                $cate_list[$key]['operate'] = showOperate($this->makeNoteCateButton($vo['id']));
                if( $vo['state'] == 1){
                    $cate_list[$key]['stateStr'] = '启用';
                }else{
                    $cate_list[$key]['stateStr'] = '禁用';
                }

            }
            $return['count'] = $cate->getAllCatesCount($where);  //总数据
            $return['code'] = "0"; //状态码
            $return['data'] = $cate_list;
            return json($return);
        }
        return $this->fetch();
    }



    /**
     * 添加帖子分类
     */
    public function addNoteCate()
    {
        if (request()->isPost()) {
            $param = input("post.");
            $cate = new ForumNoteCateModel();
            $flag = $cate->insertCate($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        return $this->fetch();
    }

    /**
     * 编辑帖子分类
     */
    public function editNoteCate()
    {
        $cate = new ForumNoteCateModel();
        if(request()->isPost()){
            $param = input("post.");
            $flag = $cate->editNoteCate($param);
            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }
        $id = input("param.id");
        $cate_info = $cate->getOneCate(['id'=>$id]);
        $this->assign([
            'cate_info' => $cate_info
        ]);
        return $this->fetch();
    }

    /**
     * 删除帖子分类
     */
    public function delNoteCate(){
        $id = input("param.id");
        $cate = new ForumNoteCateModel();
        $flag  = $cate->delNoteCate($id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }


    /**
     * 帖子分类拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeNoteCateButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'forum/editNoteCate',
                'href' => url('forum/editNoteCate', ['id' => $id]),
                'btnStyle' => 'normal',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'forum/delNoteCate',
                'href' => "javascript:delNoteCate(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
        ];

    }
    /*************************帖子分类管理***结束**************************/

    /**
     * 帖子管理
     */
    public function note(){

    }

}